﻿#include "InsertDevModeDlg.h"
#include "ui_InsertDevModeDlg.h"
#include <QDir>
#include <QList>
#include <QStringList>
#include <QAbstractItemModel>
#include <QDateTime>
#include <QFile>
#include <QTextStream>
#include <QMessageBox>
#include <QByteArray>
#include <QStringList>

InsertDevModeDlg::InsertDevModeDlg(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::InsertDevModeDlg)
{
	m_SeltFileNo = -1;
	m_ModelYxNum = 0;
	m_ModelYcNum = 0;
	m_ModelBhycNum = 0;
	m_ModelYkNum = 0;
	m_ModelYmNum = 0;
	m_ModelSetNum = 0;
	m_ModelParaNum = 0;
	m_ModelPrecNum = 0;
	m_ModelRybNum = 0;

 
	m_ModelFileName.clear();
    ui->setupUi(this);
	setFixedSize(622, 400);
	setWindowTitle(QString::fromLocal8Bit("添加普通设备模板"));

	ui->pModelFileTabWdgt->setCornerButtonEnabled(false);
	ui->pModelFileTabWdgt->verticalHeader()->setResizeMode(QHeaderView::Fixed);
	ui->pModelFileTabWdgt->setAlternatingRowColors(true);
	ui->pModelFileTabWdgt->setEditTriggers(QAbstractItemView::NoEditTriggers);
	ui->pModelFileTabWdgt->setSelectionBehavior(QAbstractItemView::SelectRows);
	ui->pModelFileTabWdgt->setSelectionMode(QAbstractItemView::SingleSelection);
	
	connect(ui->pModelFileTabWdgt, SIGNAL(itemClicked(QTableWidgetItem*)), this, SLOT(ModelFileClicked(QTableWidgetItem *)));
	if (ui->pModelFileTabWdgt->verticalHeader())
	{
		connect(ui->pModelFileTabWdgt->verticalHeader(), SIGNAL(sectionClicked(int)), this, SLOT(OnClickFileVHead(int)));
		connect(ui->pModelFileTabWdgt->verticalHeader(), SIGNAL(sectionDoubleClicked(int)), this, SLOT(OnClickFileVHead(int)));
	}
	connect(ui->pRefrashFileBtn , SIGNAL(clicked()), this, SLOT(RefrashModelFile()));
	connect(ui->pAddPushBtn , SIGNAL(clicked()), this, SLOT(AddDeviceMode()));
	connect(ui->pCacellPushBtn, SIGNAL(clicked()), this, SLOT(close()));
//	connect(ui->pAddOldModelPBtn, SIGNAL(clicked()), this, SLOT(AddOldDevMode()));
	
	InitModeFileTab();
	GetModelFileInfo();
}

InsertDevModeDlg::~InsertDevModeDlg()
{
    delete ui;
}

void InsertDevModeDlg::changeEvent(QEvent *e)
{
    QDialog::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        ui->retranslateUi(this);
        break;
    default:
        break;
    }
}

//	初始化TABLE控件
void InsertDevModeDlg::InitModeFileTab()
{
	QTableWidgetItem *HeadItem = NULL;
	ui->pModelFileTabWdgt->setColumnCount(2);

	HeadItem = new QTableWidgetItem;
	HeadItem->setText(QString::fromLocal8Bit("模板文件"));
	ui->pModelFileTabWdgt->setColumnWidth(0, 137);
	HeadItem->setTextAlignment(Qt::AlignLeft);
	ui->pModelFileTabWdgt->setHorizontalHeaderItem(0, HeadItem);

	HeadItem = new QTableWidgetItem;
	HeadItem->setText(QString::fromLocal8Bit("修改时间"));
	ui->pModelFileTabWdgt->setColumnWidth(1, 130);
	HeadItem->setTextAlignment(Qt::AlignLeft);
	ui->pModelFileTabWdgt->setHorizontalHeaderItem(1, HeadItem);
}

//	获取设备模板信息
int InsertDevModeDlg::GetModelFileInfo()
{
	unsigned int	ItemData =0;
	QString ItemText;
	QTableWidgetItem *pItem = NULL;

	QString dirStr;
	dirStr  = QString::fromLocal8Bit(DB_CFG_DEFFILE_PATH);
	QDir  DevModelDir(dirStr); 
	QStringList filters;
	filters << "*.def";
	DevModelDir.setNameFilters(filters);
	m_FileList.clear();
	m_FileList = DevModelDir.entryInfoList(QDir::Files);
	int FileCount = 0;
	FileCount = m_FileList.count();

	QAbstractItemModel *pModel = NULL;
	pModel = ui->pModelFileTabWdgt->model();
	if (pModel)
	{
		int RowCount = 0;
		RowCount = pModel->rowCount(ui->pModelFileTabWdgt->rootIndex());
		if (RowCount>0)
		{
			pModel->removeRows(0, RowCount, ui->pModelFileTabWdgt->rootIndex());
		}
	}

	if (FileCount>0)
	{
		for (int i=0; i<FileCount; i++)
		{
			ui->pModelFileTabWdgt->insertRow(i);
			ItemData = i;
			pItem = new QTableWidgetItem;
			ItemText = m_FileList[i].fileName();
			pItem->setText(ItemText);
			pItem->setData(Qt::UserRole, QVariant(ItemData));
			ui->pModelFileTabWdgt->setItem(i, 0, pItem);

			pItem = new QTableWidgetItem;
			ItemText = m_FileList[i].lastModified().toString(Qt::ISODate);
			pItem->setText(ItemText);
			pItem->setData(Qt::UserRole, QVariant(ItemData));
			ui->pModelFileTabWdgt->setItem(i, 1, pItem);
		}
	}

	return 1;
}

//	初始化选中模板文件信息
void InsertDevModeDlg::InitSeltModeFileInfo()
{
	m_ModelYxNum = 0;
	m_ModelYcNum = 0;
	m_ModelBhycNum = 0;
	m_ModelYkNum = 0;
	m_ModelYmNum = 0;
	m_ModelSetNum = 0;
	m_ModelParaNum = 0;
	m_ModelPrecNum = 0;
	m_ModelRybNum = 0;
	ui->pSeltModelFileEdt->setText("");
	m_ModelAllFileName.clear();
	m_ModelFileName.clear();
	ui->pYxNumEdit->setText("");
	ui->pYcNumEdit->setText("");
	ui->pBhycNumEdit->setText("");
	ui->pYkNumEdit->setText("");
	ui->pYmNumEdit->setText("");
	ui->pSetNumEdit->setText("");
	ui->pParaNumEdit->setText("");


}

//	初始化选中模板文件
void InsertDevModeDlg::InitSeltModeFile()
{
	InitSeltModeFileInfo();
	if (m_SeltFileNo>=0 && m_SeltFileNo<m_FileList.count())
	{
		QString attname;
		int	Ret =0;
		char strbuff[TEMP_BUFF_LEN];
		attname = QString::fromLocal8Bit("NSA3000T");
		memset(strbuff, 0, TEMP_BUFF_LEN);
		m_ModelAllFileName = m_FileList[m_SeltFileNo].absoluteFilePath();
		m_ModelFileName = m_FileList[m_SeltFileNo].fileName();
		ui->pSeltModelFileEdt->setText(m_ModelFileName);
		attname =m_ModelFileName.remove(QString(".def"), Qt::CaseInsensitive);
		//Ret = GetRelayExp(m_ModelAllFileName, attname, 1, strbuff, TEMP_BUFF_LEN);

		bool  bCheckOk = false;
		bCheckOk = CheckTemplateIndexNum();
		if (bCheckOk)
		{	
			ui->pYxNumEdit->setText(QString::fromLocal8Bit("%1").arg(m_ModelYxNum));
			ui->pYcNumEdit->setText(QString::fromLocal8Bit("%1").arg(m_ModelYcNum));
			ui->pBhycNumEdit->setText(QString::fromLocal8Bit("%1").arg(m_ModelBhycNum));
			ui->pYkNumEdit->setText(QString::fromLocal8Bit("%1").arg(m_ModelYkNum));
			ui->pYmNumEdit->setText(QString::fromLocal8Bit("%1").arg(m_ModelYmNum));
			ui->pSetNumEdit->setText(QString::fromLocal8Bit("%1").arg(m_ModelSetNum));
			ui->pParaNumEdit->setText(QString::fromLocal8Bit("%1").arg(m_ModelParaNum));
			ui->pPrecNumEdit->setText(QString::fromLocal8Bit("%1").arg(m_ModelPrecNum));//
 			ui->pRybNumEdit->setText(QString::fromLocal8Bit("%1").arg(m_ModelRybNum));
 //			ui->pRlySOENumEdit->setText(QString::fromLocal8Bit("%1").arg(m_ModelRlySOENum));
 //			ui->pCheckSelfNumEdit->setText(QString::fromLocal8Bit("%1").arg(m_ModelCheckSelfNum));
 //			ui->pRlySamplingNumEdit->setText(QString::fromLocal8Bit("%1").arg(m_ModelRlySamplingNum));
		}
		else
		{
			QMessageBox::warning(this,QString::fromLocal8Bit("检查模板错误"),QString::fromLocal8Bit("获取四遥个数错误"));
		}

	}
}

void InsertDevModeDlg::ModelFileClicked(QTableWidgetItem *pItem)
{
	if (pItem)
	{
		int RowNo = 0;
		RowNo = pItem->row();
		OnClickFileVHead(RowNo);
	}
}

void InsertDevModeDlg::OnClickFileVHead(int RowNo)
{
	if (RowNo>=0 && RowNo<ui->pModelFileTabWdgt->rowCount())
	{
		QTableWidgetItem *pItem = NULL;
		pItem = ui->pModelFileTabWdgt->item(RowNo, 0);
		if (pItem)
		{
			m_SeltFileNo = RowNo;
			InitSeltModeFile();
		}
	}
}

void InsertDevModeDlg::RefrashModelFile()
{
	m_SeltFileNo = -1;
	GetModelFileInfo();
}	

void InsertDevModeDlg::AddDeviceMode()
{
//	getPrivateProfileString()

	QString ErrInfo;
	bool  bCheckOk = false;
	int	Ret = 0;
	if (m_ModelFileName.length()<=0 || m_ModelAllFileName.length()<=0)
	{
		QMessageBox::information(NULL, QString::fromLocal8Bit("数据库组态"), QString::fromLocal8Bit("未选择模板文件！"));
		return;
	}

	bCheckOk = bCheckTemplate(ErrInfo);
	if (!bCheckOk)
	{
		QMessageBox::information(NULL, QString::fromLocal8Bit("数据库组态"), ErrInfo);
		return ;
	}

	rDevModel_Info DevModel;
	QString      ModelName;

	m_ColInfo.InitColumnInfo(SYSTEM_TYPE_NSA_YD, TABLE_RDEVMODEL_INFO);
	if (!m_ColInfo.IsSafe())
	{
		m_ColInfo.ReleasColumnInfo();
		return ;
	}
	ModelName = m_ModelFileName.remove(QString(".def"), Qt::CaseInsensitive);
	g_DbCfgFunObj.SetDefaultData((char*)&DevModel, m_ColInfo.m_pColumnInfo, m_ColInfo.m_pDataOffset, m_ColInfo.m_columnNum, m_ColInfo.m_RecordLength, 0);
	
	sprintf(DevModel.Name, "%s", ModelName.toLocal8Bit().data());
	if (!IsDevModelNameSafte(DevModel.Name))
	{
		m_ColInfo.ReleasColumnInfo();
		return ;
	}

	DevModel.YxNum	 = m_ModelYxNum;
	DevModel.YcNum	 = m_ModelYcNum;
	DevModel.BhycNum = m_ModelBhycNum;
	DevModel.YkNum	 = m_ModelYkNum;
	DevModel.YmNum	 = m_ModelYmNum;
	DevModel.SetNum	 = m_ModelSetNum;
	DevModel.ParaNum = m_ModelParaNum;
	DevModel.PrecNum = m_ModelPrecNum;
	DevModel.RybNum  = m_ModelRybNum;
	DevModel.RybNum  = m_ModelSetAreaNum;
	unsigned int RecordId = 0;
	if (g_DbCfgFunObj.GetNewFreeId(SYSTEM_TYPE_NSA_YD, TABLE_RDEVMODEL_INFO, RecordId)>0)
	{
		DevModel.ID = RecordId;
	}else
	{
		QMessageBox::information(this, QString::fromLocal8Bit("数据库组态"), QString::fromLocal8Bit("获取空闲设备模板ID失败！"), QMessageBox::Ok | QMessageBox::Cancel);
		m_ColInfo.ReleasColumnInfo();
		return ;
	}

	char tableName[TABLE_ENG_NAME_LEN];
	memset(tableName, 0, TABLE_ENG_NAME_LEN);
	sprintf(tableName, "rDevModel_Info");
	Ret = g_DbCfgFunObj.InsertRecordIntoDB(SYSTEM_TYPE_NSA_YD, TABLE_RDEVMODEL_INFO, (char*)&DevModel, m_ColInfo.m_RecordLength, m_ColInfo.m_pColumnInfo, m_ColInfo.m_pDataOffset, m_ColInfo.m_columnNum, tableName);
	if (Ret<0)
	{
		QMessageBox::information(this, QString::fromLocal8Bit("数据库组态"), QString::fromLocal8Bit("插入设备模板失败！"), QMessageBox::Ok | QMessageBox::Cancel);
		m_ColInfo.ReleasColumnInfo();
		return ;
	}

	m_ColInfo.ReleasColumnInfo();
	
	int newModelYxNum	= 0;
	int newModelYcNum	= 0;
	int newModelBhycNum = 0;
	int newModelYkNum	= 0;
	int newModelYmNum	= 0;
	int newModelSetNum	= 0;
	int newModelParaNum = 0;
	int newModelPrecNum = 0;
	int newModelRybNum  = 0;
	int newModelAreaNum  = 0;
	g_pSqLiteDB.BeginTransaction();
	newModelYxNum	= InsertModel_Yx(DevModel.ID);
	newModelYcNum	= InsertModel_Yc(DevModel.ID);
	newModelBhycNum = InsertModel_Bhyc(DevModel.ID);
	newModelYkNum	= InsertModel_Yk(DevModel.ID);
	newModelYmNum	= InsertModel_Ym(DevModel.ID);
	newModelSetNum	= InsertModel_Set(DevModel.ID);
	newModelParaNum = InsertModel_Para(DevModel.ID);
	newModelPrecNum = InsertModel_Prec(DevModel.ID);
	newModelRybNum  = InsertModel_Ryb(DevModel.ID);
	newModelAreaNum  = InsertModel_SetArea(DevModel.ID);
	g_pSqLiteDB.CommitTransaction();
	emit UpDateDevModeItem_Signal(SYSTEM_TYPE_NSA_YD, TABLE_RDEVMODEL_INFO, 0, DevModel.ID, OPER_ROW_INSERT);
	emit RefreshData_Signal();
	QString Str;
	Str = QString::fromLocal8Bit("插入设备模板“%1”: \r\n YxNum:%2,	YcNum:%3\r\n YkNum:%4,	YmNum:%5\r\n SetNum:%6,	ParaNum:%7\r\n BhycNum:%8,	PrecNum:%9\r\n	RybNum:%10，AreaNum:%10")
		.arg(QString::fromLocal8Bit(DevModel.Name))
		.arg(newModelYxNum)
		.arg(newModelYcNum)
		.arg(newModelYkNum)
		.arg(newModelYmNum)
		.arg(newModelSetNum)
		.arg(newModelParaNum)
		.arg(newModelBhycNum)
		.arg(newModelPrecNum)
		.arg(newModelRybNum)
		.arg(newModelAreaNum);
	QMessageBox::information(this, QString::fromLocal8Bit("数据库组态"), Str, QMessageBox::Ok | QMessageBox::Cancel);
}

//设备模板名字是否有重复
bool InsertDevModeDlg::IsDevModelNameSafte(char *pByName)
{
	if (!pByName)
	{
		return false;
	}

	QString Str;
	Str = QString::fromLocal8Bit(pByName);
	Str = Str.trimmed();

	if (Str.contains(' ', Qt::CaseInsensitive))
	{
		QMessageBox::information(NULL, QString::fromLocal8Bit("数据库组态"), QString::fromLocal8Bit("名称中有空格！"));
		return false;
	}
	if (Str.length() > STRING_NORMAL_LEN)
	{
		QMessageBox::information(NULL, QString::fromLocal8Bit("数据库组态"), QString::fromLocal8Bit("名称太长，不允许超过50字节！"));
		return false;
	}
	sprintf(pByName, "%s", Str.toLocal8Bit().data());

	char	sql[SQL_MAX_LEN];
	char	*buff = NULL;
	int	buff_len = 0;
	int	Count = 0;
	int	Result = 0;
	memset(sql, 0, SQL_MAX_LEN);

	sprintf(sql, "select count(*) from rDevModel_Info where Name='%s' ", Str.toLocal8Bit().data());

	Result = g_pSqLiteDB.DB_SelectUInt(sql, buff, &buff_len);

	if (Result > 0 && buff && buff_len>0)
	{
		memcpy(&Count, buff, sizeof(int));
	}

	if (buff)
	{
		delete []buff;
		buff = NULL;
	}

	if (Count>0)
	{
		QMessageBox::information(NULL, QString::fromLocal8Bit("数据库组态"), QString::fromLocal8Bit("模板名字[%1]重复！").arg(Str));
		return false;
	}else
	{
		return true;
	}
	return false;
}

//	读模板类型信息
int InsertDevModeDlg::GetRelayExp(QString filename, QString attname, int attno, char *pbuff, int buff_size)
{
	if (filename.isEmpty() || attname.isEmpty() || !pbuff || buff_size<=0)
	{
		return -1;
	}

	memset(pbuff, 0, buff_size);
	QString Num;
	QString s;
	Num = QString::fromLocal8Bit("%1").arg(attno);
	s = QString::fromLocal8Bit("");
	
	return m_FileOper.GetPrivateProfileString_My(attname, Num, s, pbuff, buff_size, filename);
}

//	读模板类型信息
int InsertDevModeDlg::GetRelayExp_Str(QString filename, QString attname, int attno, QString &ContStr)
{
	if (filename.isEmpty() || attname.isEmpty())
	{
		return -1;
	}
	ContStr.clear();
	QString Num;
	QString s;
	Num = QString::fromLocal8Bit("%1").arg(attno);
	s = QString::fromLocal8Bit("");

	return m_FileOper.GetPrivateProfileString_My(attname, Num, s, ContStr, filename);
}


bool InsertDevModeDlg::bCheckTemplate(QString &ErrInfo)
{
	if (m_ModelAllFileName.isEmpty())
	{
		return false;
	}
	
	char	buff[TEMP_BUFF_LEN];
	int	i = 0;
	int	Ret = 0;
	QString str;
	QString attname;
	//遥信
	if (m_ModelYxNum > 0)
	{
		attname = QString::fromLocal8Bit("遥信量");
		for (i=1; i<=m_ModelYxNum; i++)
		{
			Ret = GetRelayExp(m_ModelAllFileName, attname, i, buff, TEMP_BUFF_LEN);
			if (Ret<=0)
			{
				ErrInfo = QString::fromLocal8Bit("遥信第%1行参数未能正确读取！").arg(i);
				return false;
			}
		}
	}

	//遥测
	if (m_ModelYcNum > 0)
	{
		attname = QString::fromLocal8Bit("遥测量");
		for (i=1; i<=m_ModelYcNum; i++)
		{
			Ret = GetRelayExp(m_ModelAllFileName, attname, i, buff, TEMP_BUFF_LEN);
			if (Ret<=0)
			{
				ErrInfo = QString::fromLocal8Bit("遥测第%1行参数未能正确读取！").arg(i);
				return false;
			}
		}
	}
	//保护测量
	if (m_ModelBhycNum > 0)
	{
		attname = QString::fromLocal8Bit("保护测量");
		for (i=1; i<=m_ModelBhycNum; i++)
		{
			Ret = GetRelayExp(m_ModelAllFileName, attname, i, buff, TEMP_BUFF_LEN);
			if (Ret<=0)
			{
				ErrInfo = QString::fromLocal8Bit("保护测量第%1行参数未能正确读取！").arg(i);
				return false;
			}
		}
	}
	//遥控
	if (m_ModelYkNum > 0)
	{
		attname = QString::fromLocal8Bit("遥控量");
		for (i=1; i<=m_ModelYkNum; i++)
		{
			Ret = GetRelayExp(m_ModelAllFileName, attname, i, buff, TEMP_BUFF_LEN);
			if (Ret<=0)
			{
				ErrInfo = QString::fromLocal8Bit("遥控第%1行参数未能正确读取！").arg(i);
				return false;
			}
		}
	}
	//遥脉
	if (m_ModelYmNum > 0)
	{
		attname = QString::fromLocal8Bit("遥脉量");
		for (i=1; i<=m_ModelYmNum; i++)
		{
			Ret = GetRelayExp(m_ModelAllFileName, attname, i, buff, TEMP_BUFF_LEN);
			if (Ret<=0)
			{
				ErrInfo = QString::fromLocal8Bit("遥脉第%1行参数未能正确读取！").arg(i);
				return false;
			}
		}
	}
	
//保护定值
	if (m_ModelSetNum > 0)
	{
		attname = QString::fromLocal8Bit("定值");
		for (i=1; i<=m_ModelSetNum; i++)
		{
			Ret = GetRelayExp(m_ModelAllFileName, attname, i, buff, TEMP_BUFF_LEN);
			if (Ret<=0)
			{
				ErrInfo = QString::fromLocal8Bit("定值第%1行参数未能正确读取！").arg(i);
				return false;
			}
		}
	}
	if (m_ModelParaNum > 0)
	{
		attname = QString::fromLocal8Bit("参数");
		for (i=1; i<=m_ModelParaNum; i++)
		{
			Ret = GetRelayExp(m_ModelAllFileName, attname, i, buff, TEMP_BUFF_LEN);
			if (Ret<=0)
			{
				ErrInfo = QString::fromLocal8Bit("参数第%1行参数未能正确读取！").arg(i);
				return false;
			}
		}
	}
	if (m_ModelPrecNum > 0)
	{
		attname = QString::fromLocal8Bit("精度");
		for (i=1; i<=m_ModelPrecNum; i++)
		{
			Ret = GetRelayExp(m_ModelAllFileName, attname, i, buff, TEMP_BUFF_LEN);
			if (Ret<=0)
			{
				ErrInfo = QString::fromLocal8Bit("精度第%1行参数未能正确读取！").arg(i);
				return false;
			}
		}
	}
	if (m_ModelRybNum > 0)
	{
		attname = QString::fromLocal8Bit("软压板");
		for (i=1; i<=m_ModelRybNum; i++)
		{
			Ret = GetRelayExp(m_ModelAllFileName, attname, i, buff, TEMP_BUFF_LEN);
			if (Ret<=0)
			{
				ErrInfo = QString::fromLocal8Bit("软压板第%1行参数未能正确读取！").arg(i);
				return false;
			}
		}
	}
// 	//动作报告
// 	if (m_ModelRlySOENum > 0)
// 	{
// 		attname = QString::fromLocal8Bit("RLYSOE");
// 		for (i=1; i<=m_ModelRlySOENum; i++)
// 		{
// 			Ret = GetRelayExp(m_ModelAllFileName, attname, i, buff, TEMP_BUFF_LEN);
// 			if (Ret<=0)
// 			{
// 				ErrInfo = QString::fromLocal8Bit("动作报告第%1行参数未能正确读取！").arg(i);
// 				return false;
// 			}
// 		}
// 	}
// 	//自检报告
// 	if (m_ModelCheckSelfNum > 0)
// 	{
// 		attname = QString::fromLocal8Bit("CHECKSELF");
// 		for (i=1; i<=m_ModelCheckSelfNum; i++)
// 		{
// 			Ret = GetRelayExp(m_ModelAllFileName, attname, i, buff, TEMP_BUFF_LEN);
// 			if (Ret<=0)
// 			{
// 				ErrInfo = QString::fromLocal8Bit("自检报告第%1行参数未能正确读取！").arg(i);
// 				return false;
// 			}
// 		}
// 	}
// 	//保护采样
// 	if (m_ModelRlySamplingNum > 0)
// 	{
// 		attname = QString::fromLocal8Bit("RLYMEASURE");
// 		for (i=1; i<=m_ModelRlySamplingNum; i++)
// 		{
// 			Ret = GetRelayExp(m_ModelAllFileName, attname, i, buff, TEMP_BUFF_LEN);
// 			if (Ret<=0)
// 			{
// 				ErrInfo = QString::fromLocal8Bit("保护采样第%1行参数未能正确读取！").arg(i);
// 				return false;
// 			}
// 		}
// 	}
	
	return true;
}

//	检查模板文件四遥个数
bool InsertDevModeDlg::CheckTemplateIndexNum()
{
	if (m_ModelAllFileName.isEmpty())
	{
		return false;
	}

	char	buff[TEMP_BUFF_LEN];
	int	i = 0;
	int	Ret = 0;
	QString str;
	QString attname;
	QStringList TempStrLst;
	//遥信
	attname = QString::fromLocal8Bit("遥信量");
	m_ModelYxNum = m_FileOper.GetPrivateProfileStringList_My(attname,TempStrLst, m_ModelAllFileName);
	TempStrLst.clear();

	//遥测
	attname = QString::fromLocal8Bit("遥测量");
	m_ModelYcNum = m_FileOper.GetPrivateProfileStringList_My(attname,TempStrLst, m_ModelAllFileName);
	TempStrLst.clear();

	//保护测量
	attname = QString::fromLocal8Bit("保护测量");
	m_ModelBhycNum = m_FileOper.GetPrivateProfileStringList_My(attname,TempStrLst, m_ModelAllFileName);
	TempStrLst.clear();

	//遥控
	attname = QString::fromLocal8Bit("遥控量");
	m_ModelYkNum = m_FileOper.GetPrivateProfileStringList_My(attname,TempStrLst, m_ModelAllFileName);
	TempStrLst.clear();

	//遥脉
	attname = QString::fromLocal8Bit("遥脉量");
	m_ModelYmNum = m_FileOper.GetPrivateProfileStringList_My(attname,TempStrLst, m_ModelAllFileName);
	TempStrLst.clear();

	
	attname = QString::fromLocal8Bit("定值");
	m_ModelSetNum = m_FileOper.GetPrivateProfileStringList_My(attname,TempStrLst, m_ModelAllFileName);
	TempStrLst.clear();

	attname = QString::fromLocal8Bit("参数");

	m_ModelParaNum = m_FileOper.GetPrivateProfileStringList_My(attname,TempStrLst, m_ModelAllFileName);
	TempStrLst.clear();
	
	attname = QString::fromLocal8Bit("精度");

	m_ModelPrecNum = m_FileOper.GetPrivateProfileStringList_My(attname,TempStrLst, m_ModelAllFileName);
	TempStrLst.clear();
	
	attname = QString::fromLocal8Bit("软压板");
	m_ModelRybNum = m_FileOper.GetPrivateProfileStringList_My(attname,TempStrLst, m_ModelAllFileName);
	TempStrLst.clear();

	attname = QString::fromLocal8Bit("定值区");
	m_ModelSetAreaNum = m_FileOper.GetPrivateProfileStringList_My(attname,TempStrLst, m_ModelAllFileName);
	TempStrLst.clear();
	
	return true;
}

//	插入模板YX记录
int InsertDevModeDlg::InsertModel_Yx(unsigned int DevMode_Id)
{
	if (m_ModelYxNum<=0)
	{
		return 0;
	}

	if (DevMode_Id<=0)
	{
		return -1;
	}

	int			Result = 0;
	rDevModel_Yx	rModel_Yx;
	int			NewModelYxNum = 0;
	int			i=0;
	unsigned int	StartRecordId = 0;
	QString			attname;
	QString			strCont;
	QStringList		list;
	QStringList		YxStrList;
	QString			SqlStr;
	QString			InsertHeadSqlStr;
	QString			InsertValSqlStr;
	QString			TempStr;

	char tableName[TABLE_ENG_NAME_LEN];
	memset(tableName, 0, TABLE_ENG_NAME_LEN);
	sprintf(tableName, "rDevModel_Yx");

	attname = QString::fromLocal8Bit("遥信量");
	m_FileYxNum = 0;
	m_FileYxNum = m_FileOper.GetPrivateProfileStringList_My(attname,YxStrList, m_ModelAllFileName);

	int MaxNum = (m_ModelYxNum>m_FileYxNum ? m_ModelYxNum:m_FileYxNum);

	if (MaxNum>0)
	{	
		m_ColInfo.InitColumnInfo(SYSTEM_TYPE_NSA_YD, TABLE_RDEVMODEL_YX);
		if (!m_ColInfo.IsSafe())
		{
			m_ColInfo.ReleasColumnInfo();
			return -2;
		}

		g_DbCfgFunObj.GetInsertHeadSql(SYSTEM_TYPE_NSA_YD, TABLE_RDEVMODEL_YX, tableName, InsertHeadSqlStr);

		SqlStr.clear();

		for (i=0; i<MaxNum; i++)
		{
			memset((char*)&rModel_Yx, 0, sizeof(rDevModel_Yx));
			g_DbCfgFunObj.SetDefaultData((char*)&rModel_Yx, m_ColInfo.m_pColumnInfo, m_ColInfo.m_pDataOffset, m_ColInfo.m_columnNum, m_ColInfo.m_RecordLength, DevMode_Id+PARENT_OFFSET*TABLE_RDEVMODEL_INFO);

			g_DbCfgFunObj.GetNewFreeId(SYSTEM_TYPE_NSA_YD, TABLE_RDEVMODEL_YX, StartRecordId);
			if (StartRecordId<=0)
			{
				break;
			}
			rModel_Yx.ID = StartRecordId;
			rModel_Yx.ParentId = DevMode_Id+PARENT_OFFSET*TABLE_RDEVMODEL_INFO;
			if ( i < YxStrList.count())
			{
				strCont.clear();

				strCont = YxStrList.at(i);
				if (strCont.contains("=",Qt::CaseInsensitive))
				{
					strCont = strCont.split("=").at(1);
				}

				list.clear();
				list = strCont.split(",", QString::KeepEmptyParts, Qt::CaseInsensitive);
				if (list.count()>0)
				{
					rModel_Yx.IndexNo = list[0].toInt();
				}
				
				if (list.count()>1)
				{
					TempStr = list[1].trimmed();
					if (TempStr.toLocal8Bit().length()>=NAME_SIZE)
					{
						memcpy(rModel_Yx.Name, TempStr.toLocal8Bit().data(), NAME_SIZE);
						rModel_Yx.Name[NAME_SIZE-1] = 0;
					}else
					{
						memcpy(rModel_Yx.Name, TempStr.toLocal8Bit().data(), TempStr.toLocal8Bit().length());
						rModel_Yx.Name[NAME_SIZE-1] = 0;
					}
				}
				if (list.count()>2)
				{
					rModel_Yx.Fun = list[2].toInt();
				}
				if (list.count()>3)
				{
					rModel_Yx.Inf = list[3].toInt();
				}
				if (list.count()>4)
				{
					rModel_Yx.YxType = list[4].toInt();
				}

			}else
			{
				sprintf(rModel_Yx.Name, "Yx_%d", i+1);
			}
			//rModel_Yx.IndexNo = NewModelYxNum+1;

			g_DbCfgFunObj.GetValuesSql((char*)&rModel_Yx, m_ColInfo.m_RecordLength, m_ColInfo.m_pColumnInfo, m_ColInfo.m_pDataOffset,m_ColInfo.m_columnNum, OPER_ROW_INSERT, InsertValSqlStr);
			if (InsertValSqlStr.length()<=0)
			{
				continue;
			}

			SqlStr = InsertHeadSqlStr+InsertValSqlStr;
			memset(m_Dsql, 0, SQL_MAX_LEN*2);
			sprintf(m_Dsql, "%s", SqlStr.toLocal8Bit().data());
			Result = g_pSqLiteDB.DB_Modify(m_Dsql);
			if (Result<0)
			{
				QMessageBox::information(this, QString::fromLocal8Bit("数据库组态"), QString::fromLocal8Bit("插入遥信模板失败！"), QMessageBox::Ok | QMessageBox::Cancel);
				m_ColInfo.ReleasColumnInfo();
				return NewModelYxNum;
				break;
			}
			SqlStr.clear();

			NewModelYxNum++;
		}
	}

	m_ColInfo.ReleasColumnInfo();
	return NewModelYxNum;
}

//	插入模板Yc记录
int  InsertDevModeDlg::InsertModel_Yc(unsigned int DevMode_Id)
{
	if (m_ModelYcNum<=0)
	{
		return 0;
	}

	if (DevMode_Id<=0)
	{
		return -1;
	}

	int				Result = 0;
	rDevModel_Yc	rModel_Yc;
	int				NewModelYcNum = 0;
	int				i=0;
	unsigned int	StartRecordId = 0;
	QString			attname;
	QString			strCont;
	QStringList		list;
	QStringList		YcStrList;
	QString			SqlStr;
	QString			InsertHeadSqlStr;
	QString			InsertValSqlStr;
	
	QString			TempStr;
	char tableName[TABLE_ENG_NAME_LEN];
	memset(tableName, 0, TABLE_ENG_NAME_LEN);
	sprintf(tableName, "rDevModel_Yc");

	
	attname = QString::fromLocal8Bit("遥测量");
	m_FileYcNum=0;
	m_FileYcNum = m_FileOper.GetPrivateProfileStringList_My(attname,YcStrList, m_ModelAllFileName);

	int MaxNum = (m_ModelYcNum>m_FileYcNum ? m_ModelYcNum:m_FileYcNum);
	if (MaxNum>0)
	{
		m_ColInfo.InitColumnInfo(SYSTEM_TYPE_NSA_YD, TABLE_RDEVMODEL_YC);
		if (!m_ColInfo.IsSafe())
		{
			m_ColInfo.ReleasColumnInfo();
			return -2;
		}
// 		g_DbCfgFunObj.GetNewFreeId(SYSTEM_TYPE_NSA_YD, TABLE_RDEVMODEL_YC, StartRecordId);
// 		if (StartRecordId<=0)
// 		{
// 			m_ColInfo.ReleasColumnInfo();
// 			return NewModelYcNum;
// 		}

		g_DbCfgFunObj.GetInsertHeadSql(SYSTEM_TYPE_NSA_YD, TABLE_RDEVMODEL_YC, tableName, InsertHeadSqlStr);

		SqlStr.clear();

		for (i=0; i<MaxNum; i++)
		{
			memset((char*)&rModel_Yc, 0, sizeof(rDevModel_Yc));
			g_DbCfgFunObj.SetDefaultData((char*)&rModel_Yc, m_ColInfo.m_pColumnInfo, m_ColInfo.m_pDataOffset, m_ColInfo.m_columnNum, m_ColInfo.m_RecordLength, DevMode_Id+PARENT_OFFSET*TABLE_RDEVMODEL_INFO);

			g_DbCfgFunObj.GetNewFreeId(SYSTEM_TYPE_NSA_YD, TABLE_RDEVMODEL_YC, StartRecordId);
			if (StartRecordId<=0)
			{
				break;
			}
			rModel_Yc.ID = StartRecordId;
			rModel_Yc.ParentId = DevMode_Id+PARENT_OFFSET*TABLE_RDEVMODEL_INFO;

			if ( i < YcStrList.count())
			{
				strCont.clear();

				strCont = YcStrList.at(i);
				if (strCont.contains("=",Qt::CaseInsensitive))
				{
					strCont = strCont.split("=").at(1);
				}

				list.clear();
				//strCont.replace(QRegExp("[\\s]+"), " ");
				list = strCont.split(",", QString::KeepEmptyParts, Qt::CaseInsensitive);
				
				if (list.count()>0)
				{
					rModel_Yc.IndexNo = list[0].toInt();
				}

				if (list.count()>1)
				{
					TempStr = list[1].trimmed();
					if (TempStr.toLocal8Bit().length()>=NAME_SIZE)
					{
						memcpy(rModel_Yc.Name, TempStr.toLocal8Bit().data(), NAME_SIZE);
						rModel_Yc.Name[NAME_SIZE-1] = 0;
					}else
					{
						memcpy(rModel_Yc.Name, TempStr.toLocal8Bit().data(), TempStr.toLocal8Bit().length());
						rModel_Yc.Name[NAME_SIZE-1] = 0;
					}
				}

				if (list.count()>2)
				{
					rModel_Yc.Fun = list[2].toInt();
				}
				if (list.count()>3)
				{
					rModel_Yc.Inf = list[3].toInt();
				}
				if (list.count()>4)
				{
					rModel_Yc.rating = list[4].toInt();
				}
				if (list.count()>5)
				{
					rModel_Yc.percent = list[5].toInt();
				}
				if (list.count()>6)
				{
					rModel_Yc.tYCLimit = list[6].toInt();
				}
				if (list.count()>7)
				{
					rModel_Yc.ratio = list[7].toFloat();
				}
				if (list.count()>8)
				{
					rModel_Yc.High_limit = list[8].toFloat();
				}
				if (list.count()>9)
				{
					rModel_Yc.Low_limit = list[9].toFloat();
				}
				if (list.count()>10)
				{
					TempStr = list[10].trimmed();
					if (TempStr.toLocal8Bit().length()>=UNIT_NAME_LEN)
					{
						memcpy(rModel_Yc.unit, TempStr.toLocal8Bit().data(), UNIT_NAME_LEN);
						rModel_Yc.unit[UNIT_NAME_LEN-1] = 0;
					}else
					{
						memcpy(rModel_Yc.unit, TempStr.toLocal8Bit().data(), TempStr.toLocal8Bit().length());
						rModel_Yc.unit[UNIT_NAME_LEN-1] = 0;
					}
				}
			}else
			{		
				sprintf(rModel_Yc.Name,"Yc_%d", i+1);
			}
			rModel_Yc.IndexNo = NewModelYcNum+1;
			g_DbCfgFunObj.GetValuesSql((char*)&rModel_Yc, m_ColInfo.m_RecordLength, m_ColInfo.m_pColumnInfo,  m_ColInfo.m_pDataOffset,  m_ColInfo.m_columnNum, OPER_ROW_INSERT, InsertValSqlStr);
			if (InsertValSqlStr.length()<=0)
			{
				continue;
			}

		
			SqlStr = InsertHeadSqlStr+InsertValSqlStr;
				memset(m_Dsql, 0, SQL_MAX_LEN*2);
				sprintf(m_Dsql, "%s", SqlStr.toLocal8Bit().data());
				Result = g_pSqLiteDB.DB_Modify(m_Dsql);
				if (Result<0)
				{
					QMessageBox::information(this, QString::fromLocal8Bit("数据库组态"), QString::fromLocal8Bit("插入遥测模板失败！"), QMessageBox::Ok | QMessageBox::Cancel);
					m_ColInfo.ReleasColumnInfo();
					return NewModelYcNum;
					break;
				}
				SqlStr.clear();

			NewModelYcNum++;
		}

	}

	m_ColInfo.ReleasColumnInfo();
	return NewModelYcNum;
}

//	插入模板Yk记录
int InsertDevModeDlg::InsertModel_Yk(unsigned int DevMode_Id)
{
	if (m_ModelYkNum <= 0)
	{
		return 0;
	}

	if (DevMode_Id<=0)
	{
		return -1;
	}
	int			Result = 0;
	rDevModel_Yk	rModel_Yk;
	int			NewModelYkNum = 0;
	int			i=0;
	unsigned int	StartRecordId = 0;
	QString			attname;
	QString			strCont;
	QStringList		list;
	QStringList		YkStrList;
	QString			SqlStr;
	QString			InsertHeadSqlStr;
	QString			InsertValSqlStr;
//	bool			bFirstRowFlag = true;
	
	QString			TempStr;
	char tableName[TABLE_ENG_NAME_LEN];
	memset(tableName, 0, TABLE_ENG_NAME_LEN);
	sprintf(tableName, "rDevModel_Yk");

	attname = QString::fromLocal8Bit("遥控量");

	m_FileYkNum = 0;
	m_FileYkNum = m_FileOper.GetPrivateProfileStringList_My(attname,YkStrList, m_ModelAllFileName);
	int MaxNum = (m_ModelYkNum>m_FileYkNum ? m_ModelYkNum: m_FileYkNum);
	if (MaxNum>0)
	{		
		m_ColInfo.InitColumnInfo(SYSTEM_TYPE_NSA_YD, TABLE_RDEVMODEL_YK);
		if (!m_ColInfo.IsSafe())
		{
			m_ColInfo.ReleasColumnInfo();
			return -2;
		}
		StartRecordId = 0;

		g_DbCfgFunObj.GetInsertHeadSql(SYSTEM_TYPE_NSA_YD, TABLE_RDEVMODEL_YK, tableName, InsertHeadSqlStr);

		SqlStr.clear();

		for (i=0; i<MaxNum; i++)
		{
			memset((char*)&rModel_Yk, 0, sizeof(rDevModel_Yk));
			g_DbCfgFunObj.SetDefaultData((char*)&rModel_Yk, m_ColInfo.m_pColumnInfo, m_ColInfo.m_pDataOffset, m_ColInfo.m_columnNum, m_ColInfo.m_RecordLength, DevMode_Id+PARENT_OFFSET*TABLE_RDEVMODEL_INFO);
			g_DbCfgFunObj.GetNewFreeId(SYSTEM_TYPE_NSA_YD, TABLE_RDEVMODEL_YK, StartRecordId);
			if (StartRecordId<=0)
			{
				break;
			}
			rModel_Yk.ID = StartRecordId;
			rModel_Yk.ParentId = DevMode_Id+PARENT_OFFSET*TABLE_RDEVMODEL_INFO;

			if ( i < YkStrList.count())
			{
				strCont.clear();

				strCont = YkStrList.at(i);

				if (!strCont.contains("=",Qt::CaseInsensitive))
				{
					continue;
				}
				strCont = strCont.split("=").at(1);

				list.clear();
				//strCont.replace(QRegExp("[\\s]+"), " ");
				list = strCont.split(",", QString::KeepEmptyParts, Qt::CaseInsensitive);

// 				for (int i=list.count()-1;i>=0;i--)
// 				{
// 					if (list.at(i)=="")
// 					{
// 						list.removeAt(i);
// 					}
// 				}
				if (list.count()>0)
				{
					rModel_Yk.IndexNo = list[0].toInt();
				}
				if (list.count()>1)
				{
					TempStr = list[1].trimmed();
					if (TempStr.toLocal8Bit().length()>=NAME_SIZE)
					{
						memcpy(rModel_Yk.Name, TempStr.toLocal8Bit().data(), NAME_SIZE);
						rModel_Yk.Name[NAME_SIZE-1] = 0;
					}else
					{
						memcpy(rModel_Yk.Name, TempStr.toLocal8Bit().data(), TempStr.toLocal8Bit().length());
						rModel_Yk.Name[NAME_SIZE-1] = 0;
					}
				}
				
				if (list.count()>2)
				{
					rModel_Yk.Fun = list[2].toInt();
				}
				if (list.count()>3)
				{
					rModel_Yk.Inf = list[3].toInt();
				}
				
			}else
			{
				sprintf(rModel_Yk.Name,"YK_%d", i+1);
			}
			rModel_Yk.IndexNo = NewModelYkNum+1;

			g_DbCfgFunObj.GetValuesSql((char*)&rModel_Yk, m_ColInfo.m_RecordLength, m_ColInfo.m_pColumnInfo,  m_ColInfo.m_pDataOffset,  m_ColInfo.m_columnNum, OPER_ROW_INSERT, InsertValSqlStr);
			if (InsertValSqlStr.length()<=0)
			{
				continue;
			}

			SqlStr =InsertHeadSqlStr + InsertValSqlStr;

			memset(m_Dsql, 0, SQL_MAX_LEN*2);
			sprintf(m_Dsql, "%s", SqlStr.toLocal8Bit().data());
			Result = g_pSqLiteDB.DB_Modify(m_Dsql);
			if (Result<0)
			{
				QMessageBox::information(this, QString::fromLocal8Bit("数据库组态"), QString::fromLocal8Bit("插入遥控模板失败！"), QMessageBox::Ok | QMessageBox::Cancel);
				m_ColInfo.ReleasColumnInfo();
				return NewModelYkNum;
				break;
			}
			SqlStr.clear();

			NewModelYkNum++;
		}
	}

	m_ColInfo.ReleasColumnInfo();
	return NewModelYkNum;
}



//	插入模板Ym记录
int InsertDevModeDlg::InsertModel_Ym(unsigned int DevMode_Id)
{
	if (m_ModelYmNum <= 0)
	{
		return 0;
	}

	if (DevMode_Id<=0)
	{
		return -1;
	}
	int			Result = 0;
	rDevModel_Ym	rModel_Ym;
	int			NewModelYmNum = 0;
	int			i=0;
	unsigned int	StartRecordId = 0;
	QString			attname;
	QString			strCont;
	QStringList		list;
	QStringList		YmStrList;
	QString			SqlStr;
	QString			InsertHeadSqlStr;
	QString			InsertValSqlStr;
	
	QString			TempStr;
	char tableName[TABLE_ENG_NAME_LEN];
	memset(tableName, 0, TABLE_ENG_NAME_LEN);
	sprintf(tableName, "rDevModel_Ym");

	m_ColInfo.InitColumnInfo(SYSTEM_TYPE_NSA_YD, TABLE_RDEVMODEL_YM);
	if (!m_ColInfo.IsSafe())
	{
		m_ColInfo.ReleasColumnInfo();
		return -2;
	}

	attname = QString::fromLocal8Bit("脉冲量");
	m_FileYmNum = 0;
	m_FileYmNum = m_FileOper.GetPrivateProfileStringList_My(attname,YmStrList, m_ModelAllFileName);
	int MaxNum = (m_ModelYmNum>m_FileYmNum ? m_ModelYmNum: m_FileYmNum);
	if (MaxNum>0)
	{
		StartRecordId = 0;
	

		g_DbCfgFunObj.GetInsertHeadSql(SYSTEM_TYPE_NSA_YD, TABLE_RDEVMODEL_YM, tableName, InsertHeadSqlStr);

		SqlStr.clear();
		for (i=0; i<MaxNum; i++)
		{
			memset((char*)&rModel_Ym, 0, sizeof(rDevModel_Ym));
			g_DbCfgFunObj.SetDefaultData((char*)&rModel_Ym, m_ColInfo.m_pColumnInfo, m_ColInfo.m_pDataOffset, m_ColInfo.m_columnNum, m_ColInfo.m_RecordLength, DevMode_Id+PARENT_OFFSET*TABLE_RDEVMODEL_INFO);

			g_DbCfgFunObj.GetNewFreeId(SYSTEM_TYPE_NSA_YD, TABLE_RDEVMODEL_YM, StartRecordId);
			if (StartRecordId<=0)
			{
				break;
			}
			rModel_Ym.ID = StartRecordId;
			rModel_Ym.ParentId = DevMode_Id+PARENT_OFFSET*TABLE_RDEVMODEL_INFO;

			strCont.clear();
			if ( i < YmStrList.count())
			{
				strCont = YmStrList.at(i);
				if (strCont.contains("=",Qt::CaseInsensitive))
				{
					strCont = strCont.split("=").at(1);
				}

				list.clear();
				//strCont.replace(QRegExp("[\\s]+"), " ");
				list = strCont.split(",", QString::KeepEmptyParts, Qt::CaseInsensitive);

// 				for (int i=list.count()-1;i>=0;i--)
// 				{
// 					if (list.at(i)=="")
// 					{
// 						list.removeAt(i);
// 					}
// 				}
				if (list.count()>0)
				{
					rModel_Ym.IndexNo = list[0].toInt();
				}
				if (list.count()>1)
				{
					TempStr = list[1].trimmed();
					if (TempStr.toLocal8Bit().length()>=NAME_SIZE)
					{
						memcpy(rModel_Ym.Name, TempStr.toLocal8Bit().data(), NAME_SIZE);
						rModel_Ym.Name[NAME_SIZE-1] = 0;
					}else
					{
						memcpy(rModel_Ym.Name, TempStr.toLocal8Bit().data(), TempStr.toLocal8Bit().length());
						rModel_Ym.Name[NAME_SIZE-1] = 0;
					}
				}

				if (list.count()>2)
				{
					rModel_Ym.Fun = list[2].toInt();
				}
				if (list.count()>3)
				{
					rModel_Ym.Inf = list[3].toInt();
				}
				
			}else
			{
				sprintf(rModel_Ym.Name,"YM_%d", i+1);
			}
			rModel_Ym.IndexNo = NewModelYmNum+1;

			g_DbCfgFunObj.GetValuesSql((char*)&rModel_Ym, m_ColInfo.m_RecordLength, m_ColInfo.m_pColumnInfo,  m_ColInfo.m_pDataOffset,  m_ColInfo.m_columnNum, OPER_ROW_INSERT, InsertValSqlStr);
			if (InsertValSqlStr.length()<=0)
			{
				continue;
			}

			SqlStr = InsertHeadSqlStr+InsertValSqlStr;
		
				memset(m_Dsql, 0, SQL_MAX_LEN*2);
				sprintf(m_Dsql, "%s", SqlStr.toLocal8Bit().data());
				Result = g_pSqLiteDB.DB_Modify(m_Dsql);
				if (Result<0)
				{
					QMessageBox::information(this, QString::fromLocal8Bit("数据库组态"), QString::fromLocal8Bit("插入遥脉模板失败！"), QMessageBox::Ok | QMessageBox::Cancel);
					m_ColInfo.ReleasColumnInfo();
					return NewModelYmNum;
					break;
				}
				SqlStr.clear();
			
			NewModelYmNum++;
		}
	}


	m_ColInfo.ReleasColumnInfo();
	return NewModelYmNum;	
}

//保护测量
int  InsertDevModeDlg::InsertModel_Bhyc(unsigned int DevMode_Id)
{
	if (m_ModelBhycNum<=0)	return 0;
	if (DevMode_Id<=0)	return -1;

	int				Result = 0;
	rDevModel_Bhyc	rModel_BHYc;
	int				NewModelBHYcNum = 0;
	int				i=0;
	unsigned int	StartRecordId = 0;	
	QString			attname;
	QString			strCont;
	QStringList		list;
	QStringList		BHYcStrList;
	QString			SqlStr;
	QString			InsertHeadSqlStr;
	QString			InsertValSqlStr;

	QString			TempStr;
	char tableName[TABLE_ENG_NAME_LEN];
	memset(tableName, 0, TABLE_ENG_NAME_LEN);
	sprintf(tableName, "rDevModel_Bhyc");

	
	attname = QString::fromLocal8Bit("保护测量");
	m_FileBHYcNum=0;
	m_FileBHYcNum = m_FileOper.GetPrivateProfileStringList_My(attname,BHYcStrList, m_ModelAllFileName);

	int MaxNum = (m_ModelBhycNum>m_FileBHYcNum ? m_ModelBhycNum:m_FileBHYcNum);
	if (MaxNum>0)
	{
		m_ColInfo.InitColumnInfo(SYSTEM_TYPE_NSA_YD, TABLE_RDEVMODEL_BHYC);
		if (!m_ColInfo.IsSafe())
		{
			m_ColInfo.ReleasColumnInfo();
			return -2;
		}
		

		g_DbCfgFunObj.GetInsertHeadSql(SYSTEM_TYPE_NSA_YD, TABLE_RDEVMODEL_BHYC, tableName, InsertHeadSqlStr);

		SqlStr.clear();

		for (i=0; i<MaxNum; i++)
		{
			memset((char*)&rModel_BHYc, 0, sizeof(rDevModel_Bhyc));
			g_DbCfgFunObj.SetDefaultData((char*)&rModel_BHYc, m_ColInfo.m_pColumnInfo, m_ColInfo.m_pDataOffset, m_ColInfo.m_columnNum, m_ColInfo.m_RecordLength, DevMode_Id+PARENT_OFFSET*TABLE_RDEVMODEL_INFO);

			g_DbCfgFunObj.GetNewFreeId(SYSTEM_TYPE_NSA_YD, TABLE_RDEVMODEL_BHYC, StartRecordId);
			if (StartRecordId<=0)
			{
				break;
			}
			rModel_BHYc.ID = StartRecordId;
			rModel_BHYc.ParentId = DevMode_Id+PARENT_OFFSET*TABLE_RDEVMODEL_INFO;

			if ( i < BHYcStrList.count())
			{
				strCont.clear();

				strCont = BHYcStrList.at(i);
				if (strCont.contains("=",Qt::CaseInsensitive))
				{
					strCont = strCont.split("=").at(1);
				}

				list.clear();
				//strCont.replace(QRegExp("[\\s]+"), " ");
				list = strCont.split(",", QString::KeepEmptyParts, Qt::CaseInsensitive);

// 				for (int i=list.count()-1;i>=0;i--)
// 				{
// 					if (list.at(i)=="")
// 					{
// 						list.removeAt(i);
// 					}
// 				}
				if (list.count()>0)
				{
					rModel_BHYc.IndexNo = list[0].toInt();
				}
				if (list.count()>1)
				{
					TempStr = list[1].trimmed();
					if (TempStr.toLocal8Bit().length()>=NAME_SIZE)
					{
						memcpy(rModel_BHYc.Name, TempStr.toLocal8Bit().data(), NAME_SIZE);
						rModel_BHYc.Name[NAME_SIZE-1] = 0;
					}else
					{
						memcpy(rModel_BHYc.Name, TempStr.toLocal8Bit().data(), TempStr.toLocal8Bit().length());
						rModel_BHYc.Name[NAME_SIZE-1] = 0;
					}
				}

				if (list.count()>2)
				{
					rModel_BHYc.Fun = list[2].toInt();
				}
				if (list.count()>3)
				{
					rModel_BHYc.Inf = list[3].toInt();
				}
				if (list.count()>4)
				{
					rModel_BHYc.rating = list[4].toInt();
				}
				if (list.count()>5)
				{
					rModel_BHYc.percent = list[5].toInt();
				}
				if (list.count()>6)
				{
					rModel_BHYc.tYCLimit = list[6].toInt();
				}
				if (list.count()>7)
				{
					rModel_BHYc.ratio = list[7].toFloat();
				}
				if (list.count()>8)
				{
					rModel_BHYc.High_limit = list[8].toFloat();
				}
				if (list.count()>9)
				{
					rModel_BHYc.Low_limit = list[9].toFloat();
				}
				if (list.count()>10)
				{
					TempStr = list[10].trimmed();
					if (TempStr.toLocal8Bit().length()>=UNIT_NAME_LEN)
					{
						memcpy(rModel_BHYc.unit, TempStr.toLocal8Bit().data(), UNIT_NAME_LEN);
						rModel_BHYc.unit[UNIT_NAME_LEN-1] = 0;
					}else
					{
						memcpy(rModel_BHYc.unit, TempStr.toLocal8Bit().data(), TempStr.toLocal8Bit().length());
						rModel_BHYc.unit[UNIT_NAME_LEN-1] = 0;
					}
				}
			}else
			{		
				sprintf(rModel_BHYc.Name,"Yc_%d", i+1);
			}
			rModel_BHYc.IndexNo = NewModelBHYcNum+1;
			g_DbCfgFunObj.GetValuesSql((char*)&rModel_BHYc, m_ColInfo.m_RecordLength, m_ColInfo.m_pColumnInfo,  m_ColInfo.m_pDataOffset,  m_ColInfo.m_columnNum, OPER_ROW_INSERT, InsertValSqlStr);
			if (InsertValSqlStr.length()<=0)
			{
				continue;
			}

			SqlStr = InsertHeadSqlStr+InsertValSqlStr;
			
				memset(m_Dsql, 0, SQL_MAX_LEN*2);
				sprintf(m_Dsql, "%s", SqlStr.toLocal8Bit().data());
				Result = g_pSqLiteDB.DB_Modify(m_Dsql);
				if (Result<0)
				{
					QMessageBox::information(this, QString::fromLocal8Bit("数据库组态"), QString::fromLocal8Bit("插入遥测模板失败！"), QMessageBox::Ok | QMessageBox::Cancel);
					m_ColInfo.ReleasColumnInfo();
					return NewModelBHYcNum;
					break;
				}
				SqlStr.clear();
			

			NewModelBHYcNum++;
		}

	}

	m_ColInfo.ReleasColumnInfo();
	return NewModelBHYcNum;
}

//	插入模板定值记录
int InsertDevModeDlg::InsertModel_Set(unsigned int DevMode_Id)
{
	if (m_ModelSetNum <= 0)
	{
		return 0;
	}

	if (DevMode_Id<=0)
	{
		return -1;
	}
	int			Result = 0;
	rDevModel_Set	rModel_Set;
	int				NewModelSetNum = 0;
	int			i=0,column=0;
	unsigned int	StartRecordId = 0;
	QString			attname;
	QString			strCont;
	QStringList		list;
	QStringList		SetStrList;
	QString			SqlStr;
	QString			InsertHeadSqlStr;
	QString			InsertValSqlStr;

	QString			TempStr;
	char tableName[TABLE_ENG_NAME_LEN];
	memset(tableName, 0, TABLE_ENG_NAME_LEN);
	sprintf(tableName, "rDevModel_Set");

	m_ColInfo.InitColumnInfo(SYSTEM_TYPE_NSA_YD, TABLE_RDEVMODEL_SET);
	if (!m_ColInfo.IsSafe())
	{
		m_ColInfo.ReleasColumnInfo();
		return -2;
	}

	attname = QString::fromLocal8Bit("定值");
	int m_FileSetNum = 0;
	m_FileSetNum = m_FileOper.GetPrivateProfileStringList_My(attname,SetStrList, m_ModelAllFileName);

	int MaxNum = (m_ModelSetNum>m_FileSetNum ? m_ModelSetNum:m_FileSetNum);

	if (MaxNum>0)
	{		
		m_ColInfo.InitColumnInfo(SYSTEM_TYPE_NSA_YD, TABLE_RDEVMODEL_SET);
		if (!m_ColInfo.IsSafe())
		{
			m_ColInfo.ReleasColumnInfo();
			return -2;
		}
		
		g_DbCfgFunObj.GetInsertHeadSql(SYSTEM_TYPE_NSA_YD, TABLE_RDEVMODEL_SET, tableName, InsertHeadSqlStr);

		SqlStr.clear();
		for (i=0; i<MaxNum; i++)
		{
			memset((char*)&rModel_Set, 0, sizeof(rDevModel_Set));
			g_DbCfgFunObj.SetDefaultData((char*)&rModel_Set, m_ColInfo.m_pColumnInfo, m_ColInfo.m_pDataOffset, m_ColInfo.m_columnNum, m_ColInfo.m_RecordLength, DevMode_Id+PARENT_OFFSET*TABLE_RDEVMODEL_INFO);
			
			g_DbCfgFunObj.GetNewFreeId(SYSTEM_TYPE_NSA_YD, TABLE_RDEVMODEL_SET, StartRecordId);
			if (StartRecordId<=0)
			{
				break;
			}
			rModel_Set.ID = StartRecordId;
			rModel_Set.ParentId = DevMode_Id+PARENT_OFFSET*TABLE_RDEVMODEL_INFO;

			if ( i < SetStrList.count())
			{
				strCont.clear();

				strCont = SetStrList.at(i);
				if (strCont.contains("=",Qt::CaseInsensitive))
				{
					strCont = strCont.split("=").at(1);
				}

				list.clear();
				//strCont.replace(QRegExp("[\\s]+"), " ");
				list = strCont.split(",", QString::KeepEmptyParts, Qt::CaseInsensitive);

// 				for (int i=list.count()-1;i>=0;i--)
// 				{
// 					if (list.at(i)=="")
// 					{
// 						list.removeAt(i);
// 					}
// 				}
				if (list.count()>0)
				{
					rModel_Set.IndexNo = list[0].toInt();
				}
				if (list.count()>1)
				{
					TempStr = list[1].trimmed();
					if (TempStr.toLocal8Bit().length()>=NAME_SIZE)
					{
						memcpy(rModel_Set.Name, TempStr.toLocal8Bit().data(), NAME_SIZE);
						rModel_Set.Name[NAME_SIZE-1] = 0;
					}else
					{
						memcpy(rModel_Set.Name, TempStr.toLocal8Bit().data(), TempStr.toLocal8Bit().length());
						rModel_Set.Name[NAME_SIZE-1] = 0;
					}
				}
				if (list.count()>2)
				{
					TempStr = list[2].trimmed();
					if (TempStr.toLocal8Bit().length()>=UNIT_NAME_LEN)
					{
						memcpy(rModel_Set.unit, TempStr.toLocal8Bit().data(), UNIT_NAME_LEN);
						rModel_Set.unit[UNIT_NAME_LEN-1] = 0;
					}else
					{
						memcpy(rModel_Set.unit, TempStr.toLocal8Bit().data(), TempStr.toLocal8Bit().length());
						rModel_Set.unit[UNIT_NAME_LEN-1] = 0;
					}
				}
				if (list.count()>3)
				{
					rModel_Set.Fun = list[3].toInt();
				}
				if (list.count()>4)
				{
					rModel_Set.Inf = list[4].toInt();
				}
				if (list.count()>5)
				{
					rModel_Set.defval = list[5].toFloat();
				}
				if (list.count()>6)
				{
					rModel_Set.min_val = list[6].toFloat();
				}
				if (list.count()>7)
				{
					rModel_Set.max_val = list[7].toFloat();
				}
				if (list.count()>8)
				{
					rModel_Set.daFormat = list[8].toInt();
				}
				if (list.count()>9)
				{
					rModel_Set.daType = list[9].toInt();
				}
				if (list.count()>10)
				{
					rModel_Set.step_val = list[10].toFloat();
				}
			}else
			{
				sprintf(rModel_Set.Name, "Set_%d", i+1);
			}

			rModel_Set.IndexNo = NewModelSetNum+1;

			g_DbCfgFunObj.GetValuesSql((char*)&rModel_Set, m_ColInfo.m_RecordLength, m_ColInfo.m_pColumnInfo, m_ColInfo.m_pDataOffset,m_ColInfo.m_columnNum, OPER_ROW_INSERT, InsertValSqlStr);
			if (InsertValSqlStr.length()<=0)
			{
				continue;
			}

			SqlStr = InsertHeadSqlStr+InsertValSqlStr;
		
			memset(m_Dsql, 0, SQL_MAX_LEN*2);
			sprintf(m_Dsql, "%s", SqlStr.toLocal8Bit().data());
			Result = g_pSqLiteDB.DB_Modify(m_Dsql);
			if (Result<0)
			{
				QMessageBox::information(this, QString::fromLocal8Bit("数据库组态"), QString::fromLocal8Bit("插入定值模板失败！"), QMessageBox::Ok | QMessageBox::Cancel);
				m_ColInfo.ReleasColumnInfo();
				return NewModelSetNum;
				break;
			}
			SqlStr.clear();

			NewModelSetNum++;
		}

	}

	m_ColInfo.ReleasColumnInfo();
	return NewModelSetNum;	
}
int InsertDevModeDlg::InsertModel_Para(unsigned int DevMode_Id)
{
	if (m_ModelParaNum <= 0)
	{
		return 0;
	}

	if (DevMode_Id<=0)
	{
		return -1;
	}
	int			Result = 0;
	rDevModel_Para	rModel_Para;
	int			NewModelNum = 0;
	int			i=0,column=0;
	unsigned int			StartRecordId = 0;
	QString			attname;
	QString			strCont;
	QStringList		list;
	QStringList		ParaStrList;
	QString			SqlStr;
	QString			InsertHeadSqlStr;
	QString			InsertValSqlStr;

	QString			TempStr;
	char tableName[TABLE_ENG_NAME_LEN];
	memset(tableName, 0, TABLE_ENG_NAME_LEN);
	sprintf(tableName, "rDevModel_Para");

	attname = QString::fromLocal8Bit("参数");
	int m_FileParaNum = 0;
	m_FileParaNum = m_FileOper.GetPrivateProfileStringList_My(attname,ParaStrList, m_ModelAllFileName);

	int MaxNum = (m_ModelParaNum>m_FileParaNum ? m_ModelParaNum:m_FileParaNum);

	if (MaxNum>0)
	{	
		m_ColInfo.InitColumnInfo(SYSTEM_TYPE_NSA_YD, TABLE_RDEVMODEL_PARA);
		if (!m_ColInfo.IsSafe())
		{
			m_ColInfo.ReleasColumnInfo();
			return -2;
		}
		
		g_DbCfgFunObj.GetInsertHeadSql(SYSTEM_TYPE_NSA_YD, TABLE_RDEVMODEL_PARA, tableName, InsertHeadSqlStr);

		SqlStr.clear();
		for (i=0; i<MaxNum; i++)
		{
			memset((char*)&rModel_Para, 0, sizeof(rDevModel_Para));
			g_DbCfgFunObj.SetDefaultData((char*)&rModel_Para, m_ColInfo.m_pColumnInfo, m_ColInfo.m_pDataOffset, m_ColInfo.m_columnNum, m_ColInfo.m_RecordLength, DevMode_Id+PARENT_OFFSET*TABLE_RDEVMODEL_INFO);

			g_DbCfgFunObj.GetNewFreeId(SYSTEM_TYPE_NSA_YD, TABLE_RDEVMODEL_PARA, StartRecordId);
			if (StartRecordId<=0)
			{
				break;
			}
			rModel_Para.ID = StartRecordId;
			rModel_Para.ParentId = DevMode_Id+PARENT_OFFSET*TABLE_RDEVMODEL_INFO;

			if ( i < ParaStrList.count())
			{
				strCont.clear();

				strCont = ParaStrList.at(i);
				if (strCont.contains("=",Qt::CaseInsensitive))
				{
					strCont = strCont.split("=").at(1);
				}

				list.clear();
				//strCont.replace(QRegExp("[\\s]+"), " ");
				list = strCont.split(",", QString::KeepEmptyParts, Qt::CaseInsensitive);

// 				for (int i=list.count()-1;i>=0;i--)
// 				{
// 					if (list.at(i)=="")
// 					{
// 						list.removeAt(i);
// 					}
// 				}
				if (list.count()>0)
				{
					rModel_Para.IndexNo = list[0].toInt();
				}
				if (list.count()>1)
				{
					TempStr = list[1].trimmed();
					if (TempStr.toLocal8Bit().length()>=NAME_SIZE)
					{
						memcpy(rModel_Para.Name, TempStr.toLocal8Bit().data(), NAME_SIZE);
						rModel_Para.Name[NAME_SIZE-1] = 0;
					}else
					{
						memcpy(rModel_Para.Name, TempStr.toLocal8Bit().data(), TempStr.toLocal8Bit().length());
						rModel_Para.Name[NAME_SIZE-1] = 0;
					}
				}
				if (list.count()>2)
				{
					TempStr = list[2].trimmed();
					if (TempStr.toLocal8Bit().length()>=UNIT_NAME_LEN)
					{
						memcpy(rModel_Para.unit, TempStr.toLocal8Bit().data(), UNIT_NAME_LEN);
						rModel_Para.unit[UNIT_NAME_LEN-1] = 0;
					}else
					{
						memcpy(rModel_Para.unit, TempStr.toLocal8Bit().data(), TempStr.toLocal8Bit().length());
						rModel_Para.unit[UNIT_NAME_LEN-1] = 0;
					}
				}
				if (list.count()>3)
				{
					rModel_Para.Fun = list[3].toInt();
				}
				if (list.count()>4)
				{
					rModel_Para.Inf = list[4].toInt();
				}
				if (list.count()>5)
				{
					rModel_Para.defval = list[5].toFloat();
				}
				if (list.count()>6)
				{
					rModel_Para.min_val = list[6].toFloat();
				}
				if (list.count()>7)
				{
					rModel_Para.max_val = list[7].toFloat();
				}
				if (list.count()>8)
				{
					rModel_Para.daFormat = list[8].toInt();
				}
				if (list.count()>9)
				{
					rModel_Para.daType = list[9].toInt();
				}
				if (list.count()>10)
				{
					rModel_Para.step_val = list[10].toFloat();
				}
			}else
			{
				sprintf(rModel_Para.Name, "Para_%d", i+1);
			}
			rModel_Para.IndexNo = NewModelNum+1;

			g_DbCfgFunObj.GetValuesSql((char*)&rModel_Para, m_ColInfo.m_RecordLength, m_ColInfo.m_pColumnInfo, m_ColInfo.m_pDataOffset,m_ColInfo.m_columnNum, OPER_ROW_INSERT, InsertValSqlStr);
			if (InsertValSqlStr.length()<=0)
			{
				continue;
			}

			SqlStr = InsertHeadSqlStr+InsertValSqlStr;
			
			memset(m_Dsql, 0, SQL_MAX_LEN*2);
			sprintf(m_Dsql, "%s", SqlStr.toLocal8Bit().data());
			Result = g_pSqLiteDB.DB_Modify(m_Dsql);
			if (Result<0)
			{
				QMessageBox::information(this, QString::fromLocal8Bit("数据库组态"), QString::fromLocal8Bit("插入参数模板失败！"), QMessageBox::Ok | QMessageBox::Cancel);
				m_ColInfo.ReleasColumnInfo();
				return NewModelNum;
				break;
			}
			SqlStr.clear();

			NewModelNum++;
		}

	}

	m_ColInfo.ReleasColumnInfo();
	return NewModelNum;	
}

int InsertDevModeDlg::InsertModel_Prec(unsigned int DevMode_Id)
{
	if (m_ModelPrecNum <= 0)
	{
		return 0;
	}

	if (DevMode_Id<=0)
	{
		return -1;
	}
	int			Result = 0;
	rDevModel_Prec	rModel_Prec;
	int			NewModelNum = 0;
	int			i=0,column=0;
	unsigned int			StartRecordId = 0;
	QString			attname;
	QString			strCont;
	QStringList		list;
	QStringList		PrecStrList;
	QString			SqlStr;
	QString			InsertHeadSqlStr;
	QString			InsertValSqlStr;

	QString			TempStr;
	
	m_ColInfo.InitColumnInfo(SYSTEM_TYPE_NSA_YD, TABLE_RDEVMODEL_PREC);
	if (!m_ColInfo.IsSafe())
	{
		m_ColInfo.ReleasColumnInfo();
		return -2;
	}
	char tableName[TABLE_ENG_NAME_LEN];
	memset(tableName, 0, TABLE_ENG_NAME_LEN);
	sprintf(tableName, "rDevModel_Prec");

	attname = QString::fromLocal8Bit("精度");
	int m_FilePrecNum = 0;
	m_FilePrecNum = m_FileOper.GetPrivateProfileStringList_My(attname,PrecStrList, m_ModelAllFileName);

	int MaxNum = (m_ModelPrecNum>m_FilePrecNum ? m_ModelPrecNum:m_FilePrecNum);


	if (MaxNum>0)
	{
		m_ColInfo.InitColumnInfo(SYSTEM_TYPE_NSA_YD, TABLE_RDEVMODEL_PREC);
		if (!m_ColInfo.IsSafe())
		{
			m_ColInfo.ReleasColumnInfo();
			return -2;
		}
	
		g_DbCfgFunObj.GetInsertHeadSql(SYSTEM_TYPE_NSA_YD, TABLE_RDEVMODEL_PREC, tableName, InsertHeadSqlStr);

		SqlStr.clear();

		for (i=0; i<MaxNum; i++)
		{
			memset((char*)&rModel_Prec, 0, sizeof(rDevModel_Prec));
			g_DbCfgFunObj.SetDefaultData((char*)&rModel_Prec, m_ColInfo.m_pColumnInfo, m_ColInfo.m_pDataOffset, m_ColInfo.m_columnNum, m_ColInfo.m_RecordLength, DevMode_Id+PARENT_OFFSET*TABLE_RDEVMODEL_INFO);

			g_DbCfgFunObj.GetNewFreeId(SYSTEM_TYPE_NSA_YD, TABLE_RDEVMODEL_PREC, StartRecordId);
			if (StartRecordId<=0)
			{
				break;
			}
			rModel_Prec.ID = StartRecordId;
			rModel_Prec.ParentId = DevMode_Id+PARENT_OFFSET*TABLE_RDEVMODEL_INFO;

			// 			memset(buff, 0, TEMP_BUFF_LEN);
			// 			Result = GetRelayExp(m_ModelAllFileName, attname, i+1, buff, TEMP_BUFF_LEN);
			strCont.clear();
			if ( i < PrecStrList.count())
			{
				strCont.clear();

				strCont = PrecStrList.at(i);
				if (strCont.contains("=",Qt::CaseInsensitive))
				{
					strCont = strCont.split("=").at(1);
				}

				list.clear();
				//strCont.replace(QRegExp("[\\s]+"), " ");
				list = strCont.split(",", QString::KeepEmptyParts, Qt::CaseInsensitive);

// 				for (int i=list.count()-1;i>=0;i--)
// 				{
// 					if (list.at(i)=="")
// 					{
// 						list.removeAt(i);
// 					}
// 				}
				if (list.count()>0)
				{
					rModel_Prec.IndexNo = list[0].toInt();
				}
				if (list.count()>1)
				{
					TempStr =list[1].trimmed();
					if (TempStr.toLocal8Bit().length()>=NAME_SIZE)
					{
						memcpy(rModel_Prec.Name, TempStr.toLocal8Bit().data(), NAME_SIZE);
						rModel_Prec.Name[NAME_SIZE-1] = 0;
					}else
					{
						memcpy(rModel_Prec.Name, TempStr.toLocal8Bit().data(), TempStr.toLocal8Bit().length());
						rModel_Prec.Name[NAME_SIZE-1] = 0;
					}
				}
// 				if (list.count()>1)
// 				{
// 					if (list[1].toLocal8Bit().length()>=UNIT_NAME_LEN)
// 					{
// 						memcpy(rModel_Prec.unit, list[1].toLocal8Bit().data(), UNIT_NAME_LEN);
// 						rModel_Prec.unit[UNIT_NAME_LEN-1] = 0;
// 					}else
// 					{
// 						memcpy(rModel_Prec.unit, list[1].toLocal8Bit().data(), list[1].toLocal8Bit().length());
// 						rModel_Prec.unit[UNIT_NAME_LEN-1] = 0;
// 					}
// 				}
				if (list.count()>2)
				{
					rModel_Prec.Fun = list[2].toInt();
				}
				if (list.count()>3)
				{
					rModel_Prec.Inf = list[3].toInt();
				}
				if (list.count()>4)
				{
					rModel_Prec.defval = list[4].toFloat();
				}
				if (list.count()>5)
				{
					rModel_Prec.min_val = list[5].toFloat();
				}
				if (list.count()>6)
				{
					rModel_Prec.max_val = list[6].toFloat();
				}

			}else
			{
				sprintf(rModel_Prec.Name, "Prec_%d", i+1);
			}
			rModel_Prec.IndexNo = NewModelNum+1;

			g_DbCfgFunObj.GetValuesSql((char*)&rModel_Prec, m_ColInfo.m_RecordLength, m_ColInfo.m_pColumnInfo, m_ColInfo.m_pDataOffset,m_ColInfo.m_columnNum, OPER_ROW_INSERT, InsertValSqlStr);
			if (InsertValSqlStr.length()<=0)
			{
				continue;
			}

			SqlStr = InsertHeadSqlStr+InsertValSqlStr;
			
				memset(m_Dsql, 0, SQL_MAX_LEN*2);
				sprintf(m_Dsql, "%s", SqlStr.toLocal8Bit().data());
				Result = g_pSqLiteDB.DB_Modify(m_Dsql);
				if (Result<0)
				{
					QMessageBox::information(this, QString::fromLocal8Bit("数据库组态"), QString::fromLocal8Bit("插入参数模板失败！"), QMessageBox::Ok | QMessageBox::Cancel);
					m_ColInfo.ReleasColumnInfo();
					return NewModelNum;
					break;
				}
				SqlStr.clear();

			NewModelNum++;
		}

	}

	m_ColInfo.ReleasColumnInfo();
	return NewModelNum;	
}


//	插入模板软压板记录
int InsertDevModeDlg::InsertModel_Ryb(unsigned int DevMode_Id)
{
	if (m_ModelRybNum<=0)
	{
		return 0;
	}

	if (DevMode_Id<=0)
	{
		return -1;
	}

	int			Result = 0;
	rDevModel_Ryb	rModel_ryb;
	int			NewModelRybNum = 0;
	int			i=0;
	unsigned int			RecordId = 0;
	QString			attname;
	QString			strCont;
	QStringList		list;
	QStringList		RybStrList;

	QString			TempStr;
	char tableName[TABLE_ENG_NAME_LEN];
	memset(tableName, 0, TABLE_ENG_NAME_LEN);
	sprintf(tableName, "rDevModel_Ryb");

	m_ColInfo.InitColumnInfo(SYSTEM_TYPE_NSA_YD, TABLE_RDEVMODEL_RYB);
	if (!m_ColInfo.IsSafe())
	{
		m_ColInfo.ReleasColumnInfo();
		return -2;
	}
	attname = QString::fromLocal8Bit("软压板");
	int m_FilerybNum = 0;
	m_FilerybNum = m_FileOper.GetPrivateProfileStringList_My(attname, RybStrList, m_ModelAllFileName);
	
	int MaxNum = (m_ModelRybNum>m_FilerybNum ? m_ModelRybNum:m_FilerybNum);
	if (m_ModelRybNum>0)
	{
		for (i=0; i<MaxNum; i++)
		{
			RecordId = 0;
			memset((char*)&rModel_ryb, 0, sizeof(rDevModel_Ryb));
			g_DbCfgFunObj.SetDefaultData((char*)&rModel_ryb, m_ColInfo.m_pColumnInfo, m_ColInfo.m_pDataOffset, m_ColInfo.m_columnNum, m_ColInfo.m_RecordLength, DevMode_Id+PARENT_OFFSET*TABLE_RDEVMODEL_INFO);
			g_DbCfgFunObj.GetNewFreeId(SYSTEM_TYPE_NSA_YD, TABLE_RDEVMODEL_RYB, RecordId);
			if (RecordId<=0)
			{
				m_ColInfo.ReleasColumnInfo();
				return -3;
				break;
			}
			rModel_ryb.ID = RecordId;
			rModel_ryb.ParentId = DevMode_Id+PARENT_OFFSET*TABLE_RDEVMODEL_INFO;
		
			if ( i < RybStrList.count())
			{
				strCont.clear();

				strCont = RybStrList.at(i);
				if (strCont.contains("=",Qt::CaseInsensitive))
				{
					strCont = strCont.split("=").at(1);
				}

				list.clear();
				//strCont.replace(QRegExp("[\\s]+"), " ");
				list = strCont.split(",", QString::KeepEmptyParts, Qt::CaseInsensitive);

// 				for (int i=list.count()-1;i>=0;i--)
// 				{
// 					if (list.at(i)=="")
// 					{
// 						list.removeAt(i);
// 					}
// 				}
				if (list.count()>0)
				{
					rModel_ryb.IndexNo = list[0].toInt();
				}
				if (list.count()>1)
				{
					TempStr = list[1].trimmed();
					if (TempStr.toLocal8Bit().length()>=NAME_SIZE)
					{
						memcpy(rModel_ryb.Name, TempStr.toLocal8Bit().data(), NAME_SIZE);
						rModel_ryb.Name[NAME_SIZE-1] = 0;
					}else
					{
						memcpy(rModel_ryb.Name, TempStr.toLocal8Bit().data(), TempStr.toLocal8Bit().length());
						rModel_ryb.Name[NAME_SIZE-1] = 0;
					}
				}
				
				if (list.count()>2)
				{
					rModel_ryb.Fun = list[2].toInt();
				}
				if (list.count()>3)
				{
					rModel_ryb.Inf = list[3].toInt();
				}

			}else
			{
				sprintf(rModel_ryb.Name, "Prec_%d", i+1);
			}
			rModel_ryb.IndexNo = NewModelRybNum+1;

			Result = g_DbCfgFunObj.InsertRecordIntoDB(SYSTEM_TYPE_NSA_YD, TABLE_RDEVMODEL_RYB, (char*)&rModel_ryb, m_ColInfo.m_RecordLength, m_ColInfo.m_pColumnInfo,  m_ColInfo.m_pDataOffset,  m_ColInfo.m_columnNum, tableName);
			if (Result<0)
			{
				QMessageBox::information(this, QString::fromLocal8Bit("数据库组态"), QString::fromLocal8Bit("插入软压板模板失败！"), QMessageBox::Ok | QMessageBox::Cancel);
				m_ColInfo.ReleasColumnInfo();
				return NewModelRybNum;
				break;
			}
			NewModelRybNum++;
		}
	}

	m_ColInfo.ReleasColumnInfo();
	return NewModelRybNum;
}

int InsertDevModeDlg::InsertModel_SetArea(unsigned int DevMode_Id)
{
	if (m_ModelSetAreaNum<=0)
	{
		return 0;
	}

	if (DevMode_Id<=0)
	{
		return -1;
	}

	int					Result = 0;
	rDevModel_SetArea	rModel_Area;
	int					NewModelAreaNum = 0;
	int			i=0;
	unsigned int			RecordId = 0;
	QString			attname;
	QString			strCont;
	QStringList		list;
	QStringList		AreaStrList;

	QString			TempStr;
	char tableName[TABLE_ENG_NAME_LEN];
	memset(tableName, 0, TABLE_ENG_NAME_LEN);
	sprintf(tableName, "rDevModel_SetArea");

	m_ColInfo.InitColumnInfo(SYSTEM_TYPE_NSA_YD, TABLE_RDEVMODEL_SETAREA);
	if (!m_ColInfo.IsSafe())
	{
		m_ColInfo.ReleasColumnInfo();
		return -2;
	}
	attname = QString::fromLocal8Bit("定值区");
	int m_FileAreaNum = 0;
	m_FileAreaNum = m_FileOper.GetPrivateProfileStringList_My(attname, AreaStrList, m_ModelAllFileName);

	int MaxNum = (m_ModelSetAreaNum>m_FileAreaNum ? m_ModelSetAreaNum:m_FileAreaNum);
	if (MaxNum>0)
	{
		for (i=0; i<MaxNum; i++)
		{
			RecordId = 0;
			memset((char*)&rModel_Area, 0, sizeof(rDevModel_SetArea));
			g_DbCfgFunObj.SetDefaultData((char*)&rModel_Area, m_ColInfo.m_pColumnInfo, m_ColInfo.m_pDataOffset, m_ColInfo.m_columnNum, m_ColInfo.m_RecordLength, DevMode_Id+PARENT_OFFSET*TABLE_RDEVMODEL_INFO);
			g_DbCfgFunObj.GetNewFreeId(SYSTEM_TYPE_NSA_YD, TABLE_RDEVMODEL_SETAREA, RecordId);
			if (RecordId<=0)
			{
				m_ColInfo.ReleasColumnInfo();
				return -3;
				break;
			}
			rModel_Area.ID = RecordId;
			rModel_Area.ParentId = DevMode_Id+PARENT_OFFSET*TABLE_RDEVMODEL_INFO;

			if ( i < AreaStrList.count())
			{
				strCont.clear();

				strCont = AreaStrList.at(i);
				if (strCont.contains("=",Qt::CaseInsensitive))
				{
					strCont = strCont.split("=").at(1);
				}

				list.clear();
				//strCont.replace(QRegExp("[\\s]+"), " ");
				list = strCont.split(",", QString::KeepEmptyParts, Qt::CaseInsensitive);
				if (list.count()>0)
				{
					rModel_Area.IndexNo = list[0].toInt();
				}
				if (list.count()>1)
				{
					TempStr = list[1].trimmed();
					if (TempStr.toLocal8Bit().length()>=NAME_SIZE)
					{
						memcpy(rModel_Area.Name, TempStr.toLocal8Bit().data(), NAME_SIZE);
						rModel_Area.Name[NAME_SIZE-1] = 0;
					}else
					{
						memcpy(rModel_Area.Name, TempStr.toLocal8Bit().data(), TempStr.toLocal8Bit().length());
						rModel_Area.Name[NAME_SIZE-1] = 0;
					}
				}

				if (list.count()>2)
				{
					rModel_Area.Fun = list[2].toInt();
				}
				if (list.count()>3)
				{
					rModel_Area.Inf = list[3].toInt();
				}
				if (list.count()>4)
				{
					rModel_Area.value = list[4].toInt();
				}
			}else
			{
				sprintf(rModel_Area.Name, "区号_%d", i+1);
			}
			rModel_Area.IndexNo = NewModelAreaNum+1;

			Result = g_DbCfgFunObj.InsertRecordIntoDB(SYSTEM_TYPE_NSA_YD, TABLE_RDEVMODEL_SETAREA, (char*)&rModel_Area, m_ColInfo.m_RecordLength, m_ColInfo.m_pColumnInfo,  m_ColInfo.m_pDataOffset,  m_ColInfo.m_columnNum, tableName);
			if (Result<0)
			{
				QMessageBox::information(this, QString::fromLocal8Bit("数据库组态"), QString::fromLocal8Bit("插入定值区模板失败！"), QMessageBox::Ok | QMessageBox::Cancel);
				m_ColInfo.ReleasColumnInfo();
				return NewModelAreaNum;
				break;
			}
			NewModelAreaNum++;
		}
	}

	m_ColInfo.ReleasColumnInfo();
	return NewModelAreaNum;

}

void InsertDevModeDlg::RefreshData_Slot()
{
	emit RefreshData_Signal();
}

void InsertDevModeDlg::UpDateDevModeItem_Slot(quint32 AppType, quint32 TableId, quint32 ParentId, quint32 RecordId, int RowProp)
{
	emit UpDateDevModeItem_Signal(AppType,TableId, ParentId,RecordId,RowProp);
}
